#!/bin/sh

#挂载ysytem 

root_fs="UUID=93b266e8-f6bb-4661-86ab-45ea77ef5a38"
root_em="UUID=0BFC-526B"
root_sd="UUID=6224-3834"
umount -f $root_fs $root_em $root_sd
dirfs=/mnt/sys
rm -f $dirfs
mkdir -p $dirfs
mount -o rw $root_fs $dirfs
rm -rfv $dirfs/*

echo "正在安装系统"



cd /
echo "Copy BIN"
tar -cf - bin | (cd $dirfs; tar -xpf -)
#echo "Copy BOOT"
#mkdir -p $dirfs/boot
#tar -cf - boot | (cd $dirfs; tar -xpf -)
echo "Create DEV"
mkdir -p $dirfs/dev
#tar -cf - dev | (cd $dirfs; tar -xpf -)
echo "Copy ETC"
tar -cf - etc | (cd $dirfs; tar -xpf -)
echo "Copy HOME"
tar -cf - home | (cd $dirfs; tar -xpf -)
echo "Copy LIB"
tar -cf - lib | (cd $dirfs; tar -xpf -)
echo "Copy LIB64"
tar -cf - lib64 | (cd $dirfs; tar -xpf -)
echo "Create MEDIA"
mkdir -p $dirfs/media
#tar -cf - media | (cd $dirfs; tar -xpf -)
echo "Create MNT"
mkdir -p $dirfs/mnt
#tar -cf - mnt | (cd $dirfs; tar -xpf -)
echo "Copy OPT"
tar -cf - opt | (cd $dirfs; tar -xpf -)
echo "Create PROC"
mkdir -p $dirfs/proc
echo "Copy ROOT"
tar -cf - root | (cd $dirfs; tar -xpf -)
echo "Create RUN"
mkdir -p $dirfs/run
echo "Copy SBIN"
tar -cf - sbin | (cd $dirfs; tar -xpf -)
echo "Copy SELINUX"
tar -cf - selinux | (cd $dirfs; tar -xpf -)
echo "Copy SRV"
tar -cf - srv | (cd $dirfs; tar -xpf -)
echo "Create SYS"
mkdir -p $dirfs/sys
echo "Create TMP"
mkdir -p $dirfs/tmp
echo "Copy USR"
tar -cf - usr | (cd $dirfs; tar -xpf -)
echo "Copy VAR"
tar -cf - var | (cd $dirfs; tar -xpf -)
sync



#/etc/fstab
cat > $dirfs/etc/fstab << EOF 
$root_fs / ext4 defaults,noatime,nodiratime,commit=600,errors=remount-ro 0 1
$root_em /boot vfat defaults 0 2
tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
echo "复制系统完成"









echo "复制boot"

bootdir=/mnt/bootdir
rm -rfv $bootdir
mkdir -p $bootdir
echo "正在修复boot"
mount -o rw $root_em $bootdir
rm -rfv $bootdir/*

cp -rfv /boot/* $bootdir && sync


#UUID=874678b0-c5fb-4e84-bc75-9389d09f0072

#配置服务启动 /etc/supervisord.d/sshd.ini
cat > $bootdir/uEnv.txt << EOF 
LINUX=/zImage
INITRD=/uInitrd

FDT=/dtb/amlogic/meson-sm1-h96-max-x3-oc.dtb
APPEND=root=$root_fs rootflags=data=writeback rw console=ttyAML0,115200n8 console=tty0 no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0

EOF


#u盘启动到emmc
cat > /boot/uEnv.txt << EOF 
LINUX=/zImage
INITRD=/uInitrd

FDT=/dtb/amlogic/meson-sm1-h96-max-x3-oc.dtb
APPEND=root=$root_fs rootflags=data=writeback rw console=ttyAML0,115200n8 console=tty0 no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0
#u盘启动
#APPEND=root=UUID=874678b0-c5fb-4e84-bc75-9389d09f0072 rootflags=data=writeback rw console=ttyAML0,115200n8 console=tty0 no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0


EOF
sync

echo "sd 引导"

bootdirs=/mnt/boot_sd
rm -rfv $bootdirs
mkdir -p $bootdirs
echo "正在修复boot"
mount -o rw $root_sd $bootdirs
rm -rfv boot_sd/*

cp -rfv /boot/* $bootdirs && sync


#UUID=874678b0-c5fb-4e84-bc75-9389d09f0072

#配置服务启动 /etc/supervisord.d/sshd.ini
cat > $boot_sd/uEnv.txt << EOF
LINUX=/zImage
INITRD=/uInitrd

FDT=/dtb/amlogic/meson-sm1-h96-max-x3-oc.dtb
APPEND=root=$root_fs rootflags=data=writeback rw console=ttyAML0,115200n8 console=tty0 no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0

EOF







cat $dirfs/etc/fstab
cat $bootdirs/uEnv.txt

